<?php
/**
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file license.txt or that can be found at 
 * http://code.google.com/p/billboardce/wiki/License.
 *
 * @package    Admin_CronController
 * @category   Controller
 * @license    http://code.google.com/p/billboardce/wiki/License New BSD License
 * @version    SVN: $Id: $
 */

/**
 * Controller to show the status of all cron jobs running in the system
 *
 * @package    Admin_CronController
 * @category   Controller
 */
class Admin_CronController extends Zend_Controller_Action 
{
    /**
     * shows all the cron jobs
     *
     */
    public function indexAction()
    {
        $this->view->acl = array(
            'add'    => false,
            'edit'   => false,
            'toggle' => $this->_helper->hasAccess('toggle')
            );

        $cs = new Ot_Cron_Status();

        $jobs = $cs->getAvailableCronJobs();
        
        $this->view->messages = $this->_helper->flashMessenger->getMessages();
        $this->view->cronjobs = $jobs;
        $this->_helper->pageTitle('admin-cron-index:title');
    }

    /**
     * Toggles the status of the selected cron job
     *
     */
    public function toggleAction()
    {
        $cs = new Ot_Cron_Status();

        $get = Zend_Registry::get('getFilter');
        
        if (!isset($get->name)) {
        	throw new Ot_Exception_Input('msg-error-nameNotSet');
        }
        
        if (!isset($get->status)) {
            $cj = $cs->find($get->name);
    
            if (is_null($cj)) {
                $cj = array(
                    'status' => 'disabled',
                    'name'   => $get->name
                    );
    
                $status = 'disabled';
            } else {
                $cj = $cj->toArray();
    
                $status = $cj['status'];
            }
        } else {
            $status = $get->status;
        }        
        
        $form = new Zend_Form();
        $form->setAction('?name=' . $get->name)
             ->setMethod('post')
             ->setAttrib('id', 'toggleCronJob');
       
        $hidden = $form->createElement('hidden', 'status');
        $hidden->setValue(($status == 'enabled') ? 'disable' : 'enable');
        $hidden->clearDecorators();
        $hidden->addDecorators(array(
            array('ViewHelper')
        ));
               
        $submit = $form->createElement('submit', 'submitButton', array('label' => 'form-button-yes'));
        $submit->setDecorators(array(
                   array('ViewHelper', array('helper' => 'formSubmit'))
                 ));
                 
        $cancel = $form->createElement('button', 'cancel', array('label' => 'form-button-cancel'));
        $cancel->setAttrib('id', 'cancel');
        $cancel->setDecorators(array(
                   array('ViewHelper', array('helper' => 'formButton'))
                ));
                        
        $form->addElements(array($hidden))
             ->setElementDecorators(array(
                  'ViewHelper',
                  'Errors',      
                  array('HtmlTag', array('tag' => 'div', 'class' => 'elm')), 
                  array('Label', array('tag' => 'span')),      
              ))
             ->addElements(array($submit, $cancel));
        
        if ($this->_request->isPost() && $form->isValid($_POST)) {
            $status = ($form->getValue('status') == 'enable') ? 'enabled' : 'disabled';

            $cs->setCronStatus($get->name, $status);

            $logOptions = array(
                        'attributeName' => 'cronName',
                        'attributeId'   => $get->name,
            );
                    
            $this->_helper->log(Zend_Log::INFO, 'cron was set to ' . $status, $logOptions);
                        
            $this->_helper->redirector->gotoUrl('/admin/cron/');
        }
        
        if ($get->name == 'all') {
            $this->view->displayName = 'admin-cron-toggle:displayName';
        } else {
            $this->view->displayName = $get->name;
        }

        $this->view->status = ($status == 'enabled') ? 'disable' : 'enable';
        $this->_helper->pageTitle('admin-cron-toggle:title');
        $this->view->form   = $form;
    }
}